1. /* sffffsub.cpp by K.Tsuru */
  2. // function ID = 711 DARDIX
  3. /*********************************
  4. SFraction class
  5. It provides the subtraction m-n.
  6. See "sfract.h" about the reduction.
  7. **********************************/
  8. #ifndef SN_H
  9. #include "sn.h"
  10. #endif
  11. SFraction FFSub(const SFraction& x, const SFraction& y){
  12. SFraction z;
  13. #if REDUCE_SIZE==0 //use Knuth's method
  14. SLong d1, d2, t, xd1;
  15. d1 = gcdL(x.den, y.den);
  16. if(d1.IsOne()) { // d1 == 1
  17. z.den = x.den*y.den;
  18. z.num = x.num*y.den - y.num*x.den;
  19. } else {
  20. xd1 = x.den/d1;
  21. t = x.num*(y.den/d1) - y.num*xd1;
  22. d2 = gcdL(t, d1);
  23. if(d2.IsOne()){
  24. z.den = xd1*y.den;
  25. z.num = t;
  26. } else {
  27. z.den = xd1*(y.den/d2);
  28. z.num = t/d2;
  29. }
  30. }
  31. if( !z.num.Sign(701) ) z.den.SetShort(1); //It becomes zero.
  32. z.reduceDone = true;
  33. return z;
  34. #else
  35. if(x.ReduceStepByStep()) {
  36. SLong d1, d2, t, xd1;
  37. d1 = gcdL(x.den, y.den);
  38. if(d1.IsOne()) { // d1 == 1
  39. z.den = x.den*y.den;
  40. z.num = x.num*y.den - y.num*x.den;
  41. } else {
  42. xd1 = x.den/d1;
  43. t = x.num*(y.den/d1) - y.num*xd1;
  44. d2 = gcdL(t, d1);
  45. if(d2.IsOne()){
  46. z.den = xd1*y.den;
  47. z.num = t;
  48. } else {
  49. z.den = xd1*(y.den/d2);
  50. z.num = t/d2;
  51. }
  52. }
  53. if( !z.num.Sign(701) ) z.den.SetShort(1); //It becomes zero.
  54. z.reduceDone = true;
  55. return z;
  56. } else {
  57. z.den = x.den*y.den;
  58. z.num = x.num*y.den - y.num*x.den;
  59. z.reduce(false);
  60. return z;
  61. }
  62. #endif
  63. }

sffffsub.cpp : last modifiled at 2017/10/20 10:38:06(1,464 bytes)
created at 2015/12/22 16:07:29
The creation time of this html file is 2017/10/21 15:10:35 (Sat Oct 21 15:10:35 2017).